﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq;
using IssueTracker.Business.Entity;

namespace IssueTracker.Data
{
    public class IssueWfHistoryDataAccess: DataAccessComponent
    {
        private IssueTrackerDBDataContext dc;

        public IssueWfHistory Create(IssueWfHistory issueWfHistory)
        {
            IssueWfHistory temp = base.Clone < IssueWfHistory >(issueWfHistory);

            using (dc = new IssueTrackerDBDataContext())
            {
                dc.ObjectTrackingEnabled = true;

                dc.IssueWfHistories.InsertOnSubmit(temp);

                try
                {
                    dc.SubmitChanges();

                    issueWfHistory = base.Clone<IssueWfHistory>(temp);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return issueWfHistory;
        }

        public IssueWfHistory Retrieve(System.Guid id)
        {
            using (dc = new IssueTrackerDBDataContext())
            {
                var list = from proj in dc.IssueWfHistories
                           where proj.Id.Equals(id)
                           select proj;

                if (list.Count<IssueWfHistory>() > 0)
                {
                    IssueWfHistory obj = list.First<IssueWfHistory>();
                    return obj;
                }
                else
                    return null;
            }
        }

        public IssueWfHistory Update(IssueWfHistory issueWfHistory)
        {

            IssueWfHistory temp = base.Clone<IssueWfHistory>(issueWfHistory);

            using (dc = new IssueTrackerDBDataContext())
            {
                dc.ObjectTrackingEnabled = true;
                dc.IssueWfHistories.Attach(temp, true);

                try
                {
                    dc.SubmitChanges();

                    issueWfHistory = base.Clone<IssueWfHistory>(temp);
                }
                catch (ChangeConflictException ccex)
                {
                    throw new ApplicationException("The project member record that you are trying to update maybe out-dated. Please refresh and try again.", ccex);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return issueWfHistory;

        }

        public void Delete(IssueWfHistory issueWfHistory)
        {

            IssueWfHistory temp = base.Clone<IssueWfHistory>(issueWfHistory);

            using (dc = new IssueTrackerDBDataContext())
            {
                dc.IssueWfHistories.Attach(temp);

                dc.IssueWfHistories.DeleteOnSubmit(temp);

                try
                {
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public IEnumerable<IssueWfHistory> List()
        {
            IEnumerable<IssueWfHistory> returnList = null;

            using (dc = new IssueTrackerDBDataContext())
            {
                returnList = dc.IssueWfHistories;

                if (returnList != null)
                    return returnList.ToList();
                else
                    return null;
            }
        }

        public IEnumerable<IssueWfHistory> ListByReferenceId(System.Guid reference)
        {
            using (dc = new IssueTrackerDBDataContext())
            {
                var list = from proj in dc.IssueWfHistories
                           where proj.IssueId.Equals(reference)
                           select proj;

                return list;
            }
        }
   
    }
}
